package com.flying.carclub.fragment;


import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheng.commonlib.loopbander.AutoScrollViewPager;
import com.cheng.commonlib.loopbander.ViewGroupIndicator;
import com.cheng.commonlib.magicrecyclerView.BaseItem;
import com.cheng.commonlib.magicrecyclerView.BaseRecyclerAdapter;
import com.cheng.commonlib.magicrecyclerView.MagicRecyclerView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.flying.carclub.R;
import com.flying.carclub.modules.home.ArticleAdapter;
import com.flying.carclub.modules.home.ArticleAdapterFrecso;
import com.flying.carclub.modules.home.HomeTopPagerAdapter;
import com.flying.carclub.modules.home.HomeTopPagerAdapterFrecso;
import com.flying.carclub.modules.home.mvp.ArticleDailyModel;
import com.flying.carclub.modules.home.mvp.ArticleModel;
import com.flying.carclub.modules.home.mvp.TopArticleModel;
import com.flying.carclub.utils.ResponseParser;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoveryFragment extends Fragment {
    private TextView emptyView;
    private SwipeRefreshLayout refreshLayout;
    private MagicRecyclerView magicRecyclerView;
    private AutoScrollViewPager scrollViewPager;
    private ViewGroupIndicator viewGroupIndicator;
    private HomeTopPagerAdapterFrecso topPagerAdapter;
    private ArticleAdapterFrecso articleAdapter;

    private SimpleDraweeView simpleDraweeView;

    private boolean initTag;

    private ArrayList<BaseItem> baseItemList = new ArrayList<>();

    public DiscoveryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.fragment_discovery, container, false);
        initView(view);
        return view;
    }

    private void initView(View contentView){
        emptyView = contentView.findViewById(R.id.empty_msg);
        refreshLayout = contentView.findViewById(R.id.refresh_home);
        simpleDraweeView = contentView.findViewById(R.id.my_image_view);


        magicRecyclerView = contentView.findViewById(R.id.recycler_home);
        View headerLayout  = magicRecyclerView.getHeaderView();
        scrollViewPager =  headerLayout.findViewById(R.id.scroll_pager);
        magicRecyclerView.setItemAnimator(new DefaultItemAnimator());
        magicRecyclerView.getItemAnimator().setChangeDuration(0);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this.getContext());
        magicRecyclerView.setLayoutManager(mLinearLayoutManager);

        viewGroupIndicator =  headerLayout.findViewById(R.id.scroll_pager_indicator);

        viewGroupIndicator.setParent(scrollViewPager);

        articleAdapter  = new ArticleAdapterFrecso(this);
        articleAdapter.setBaseDatas(baseItemList);
        magicRecyclerView.setAdapter(articleAdapter);

        refreshView();

//        simpleDraweeView.setImageResource(R.drawable.donghua);
//        simpleDraweeView.setImageURI(Uri.parse("http://img.wanchezhijia.com/A/2015/5/6/9/48/c6344b4e-07a8-48a4-b5b9-b08cc7269d62.jpg"));
        simpleDraweeView.setImageURI(Uri.parse("http://p5.qhimg.com/t01d0e0384b952ed7e8.gif"));


       /* Uri uri =Uri.parse("res://"+getActivity().getPackageName()+"/" + R.drawable.donghua);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setAutoPlayAnimations(true)
                .setOldController(simpleDraweeView.getController())
                .build();
        simpleDraweeView.setController(controller);*/
    }

    private void refreshView(){
        ArticleDailyModel articleDailyModel = ResponseParser.getArticleList();
        ArrayList<TopArticleModel> stories = articleDailyModel.getTopArticleArrayList();
        ArrayList<ArticleModel> articleModelList =  articleDailyModel.getArticleModelList();

        if (articleModelList != null && articleModelList.size() <= 0){
            emptyView.setVisibility(View.VISIBLE);
            magicRecyclerView.setVisibility(View.INVISIBLE);
            refreshLayout.requestFocus();
        }else {
            emptyView.setVisibility(View.GONE);
            magicRecyclerView.setVisibility(View.VISIBLE);
        }
        baseItemList.clear();

        //配置顶部故事
        if (topPagerAdapter == null) {
            topPagerAdapter = new HomeTopPagerAdapterFrecso(this, stories);
            scrollViewPager.setAdapter(topPagerAdapter);
        } else {
            topPagerAdapter.resetData(stories);
            topPagerAdapter.notifyDataSetChanged();
        }
        // 配置底部文章列表
        for (ArticleModel articleModel : articleModelList){
            if (!initTag){
                initTag = true;
                BaseItem<String>  baseItem = new BaseItem<>();
                baseItem.setData(articleDailyModel.getDate());
                baseItem.setItemType(BaseRecyclerAdapter.RecyclerItemType.TYPE_TAGS);
                baseItemList.add(baseItem);
            }
            BaseItem<ArticleModel> baseItem = new BaseItem<>();
            baseItem.setData(articleModel);
            baseItemList.add(baseItem);
        }
        if (baseItemList.size() != 0){
            articleAdapter.setBaseDatas(baseItemList);
        }

    }

}
